package com.baiye.template.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.baiye.template.common.AliPayProperties;
import com.baiye.template.pojo.OrderVo;
import com.google.gson.Gson;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/17 14:40
 */
/* 支付宝 */
public class AlipayUtil {
    public static String connect(OrderVo orderVo) {
        //1、获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AliPayProperties.GATEWAYURL,//支付宝网关
                AliPayProperties.APP_ID,//appid
                AliPayProperties.MERCHANT_PRIVATE_KEY,//商户私钥
                AliPayProperties.FORMAT,
                AliPayProperties.CHARSET,//字符编码格式
                AliPayProperties.ALIPAY_PUBLIC_KEY,//支付宝公钥
                AliPayProperties.SIGN_TYPE//签名方式
        );
        //2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(AliPayProperties.RETURN_URL);
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(AliPayProperties.NOTIFY_URL);
        //封装参数
        alipayRequest.setBizContent(new Gson().toJson(orderVo));

        //3、请求支付宝进行付款，并获取支付结果
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //返回付款信息
        return result;
    }
}

